package com.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.entity.Setup;
import com.cms.mapper.SetupMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 配置参数表 服务实现类
 * </p>
 *
 * @author songfayuan
 * @since 2021-11-12
 */
@Service
public class SetupService extends ServiceImpl<SetupMapper, Setup> {

    /**
     * 获取配置
     */
    public Map<String,String> getSetupMap(){
        List<Setup> setups = findAll();
        Map<String,String> setupMap = new HashMap<String, String>();
        for(Setup setup : setups){
            setupMap.put(setup.getName(), setup.getValue());
        }
        return setupMap;
    }

    /**
     * 查找全部配置
     */
    public List<Setup> findAll(){
        QueryWrapper<Setup> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("sort");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据名称查找配置
     *
     * @return 配置
     */
    public Setup findByName(String name){
        QueryWrapper<Setup> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        return baseMapper.selectOne(queryWrapper);
    }
}
